# -*- coding: utf-8 -*-
from urllib import parse
import scrapy
from scrapy import Request
from ArticleSpider.items import ArticleItem, ArticleItemLoader
from ArticleSpider.utils.common import get_md5


class JobboleSpider(scrapy.Spider):

    name = 'jobbole'
    allowed_domains = ['blog.jobbole.com']
    start_urls = ['http://blog.jobbole.com/all-posts/']

    def parse(self, response):
        post_nodes = response.css("#archive .floated-thumb .post-thumb a")
        for post_node in post_nodes:
            image_url = post_node.css("img::attr(src)").extract_first("")
            post_url = post_node.css("::attr(href)").extract_first("")
            yield Request(url=parse.urljoin(response.url, post_url), meta={"front_image": image_url},
                          callback=self.parse_detail)
        #获取下一页
        next_url = response.css(".next.page-numbers::attr(href)").extract_first()
        if next_url:
            yield Request(next_url, callback=self.parse)
        pass


    def parse_detail(self,response):
        front_img = response.meta.get("front_image","")
        # 通过 ItemLoad 方式 构建 item
        item_load = ArticleItemLoader(item=ArticleItem(),response=response)
        item_load.add_css("title",".entry-header h1::text")
        item_load.add_xpath("create_date",'//p[@class="entry-meta-hide-on-mobile"]/text()')
        item_load.add_css("fav_nums",'.post-adds .btn-bluet-bigger h10::text')
        item_load.add_xpath("praise_nums",'//span[contains(@class,"bookmark-btn")]/text()')
        item_load.add_xpath("cmment_nums",'//a[@href="#article-comment"]/span/text()')
        item_load.add_xpath("content","//div[@class='entry']")
        item_load.add_value("url",response.url)
        item_load.add_value("front_img",[front_img])
        item_load.add_value("url_object_id",get_md5(response.url))
        item_load.add_xpath("tags","//p[@class='entry-meta-hide-on-mobile']/a/text()")
        artile_item = item_load.load_item()
        yield artile_item